import React, { Component } from 'react';
import { Card, message, Table } from 'antd';
import LinkButton from './LinkButton';

export default class Advice extends Component {
    state = {
        loading: false,
        adviceList: []
    }
    initColumns = () => {
        this.columns = [
            {
                title: '意见反馈',
                dataIndex: 'advise_advise',
            },
            {
                title: '状态',
                dataIndex: 'advise_state',
            },
            {
                title: '操作',
                render: (adviseObj) => <span>
                    <LinkButton onClick={()=>{
                        fetch('http://82.157.94.115:2005/updateadvice', {
                            method: 'POST',
                            body: JSON.stringify(adviseObj),
                            headers: {
                                'Content-Type': 'application/json'
                            }
                        })
                        .then(res => res.json())
                        .then(res => {
                            this.getAdvice();
                            message.success('您已读该条建议！');
                        })
                    }}>处理</LinkButton>
                    <LinkButton onClick={()=>{
                        fetch('http://82.157.94.115:2005/delectadvice', {
                            method: 'POST',
                            body: JSON.stringify(adviseObj),
                            headers: {
                                'Content-Type': 'application/json'
                            }
                        })
                        .then(res => res.json())
                        .then(res => {
                            this.getAdvice();
                            message.success('删除成功！');
                        })
                        .catch(error => {
                            message.error('删除失败！')
                        })
                    }}>删除</LinkButton>
                </span>
            },
        ]
    }
    getAdvice = () => {
        fetch('http://82.157.94.115:2005/advice')
            .then(res => res.json())
            .then(res => {
                this.setState({ adviceList: res.adviseObj });
            })
    }
    componentDidMount() {
        this.getAdvice();
    }
    componentWillMount() {
        this.initColumns();
    }
    render() {
        const { adviceList, loading } = this.state;
        return (
            <div>
                <Card>
                <Table
                    bordered={true}
                    columns={this.columns}
                    loading={loading}
                    dataSource={adviceList}
                    pagination={{ defaultPageSize: 10, showQuickJumper: true }}
                />
            </Card>
            </div>
        )
    }
}
